// packageA/pages/afterCost/afterCost.js
const app = getApp();
import CommonService from '../../../utils/service/commonService.js';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
const util = require('../../../utils/util');
import Base64 from '../../../utils/Base64.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //传参
    paramObj:{},
    //折叠面板
    activeNames: [],
    //配件和上门费用
    list:[],
    //物流费用
    logisticsList:[],
    //是否显示信息
    showNothing:true,
    //费用初始值
    platformCostsTotal:0,
    clientCostsTotal:0,
    logisticsTotal:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.paramObj){
      let paramObj=JSON.parse(options.paramObj);

      console.log('售后费用传参');
      console.log(paramObj);

      this.setData({
        paramObj: paramObj
      })

      //获取配件和上门费用
      this.getCostList();

      //获取物流费用
      this.getLogistics();
    }
  },

  //获取配件和上门费用
  getCostList(){
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '加载中...'
    });

    let param = {
      isAuth: false,
      className: 'afterSaleCostsRecord',
      data: {
        searchAll: true,
        afterId: this.data.paramObj.afterId,
        selectFields: ['id','name','platformCosts','clientCosts','type'],
        nbl:Base64.encode(util.timeFormatDay(new Date()))
      }
    }

    CommonService.goFindList(param).then(res=>{
      Toast.clear();
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        this.setData({
          showNothing: false
        })

        let result=res.data.data.result;
        console.log('配件和上门费用');
        console.log(result);

        //计算总费用
        let platformCostsTotal=0,
            clientCostsTotal=0;
        result.forEach(item => {
          platformCostsTotal+=item.platformCosts.total;
          clientCostsTotal+=item.clientCosts.total;
        });

        this.setData({
          list: result,
          platformCostsTotal:platformCostsTotal,
          clientCostsTotal:clientCostsTotal
        })
      }
    }).catch((error) => {
    })
  },

  //获取物流费用
  getLogistics(){
    let param = {
      isAuth: false,
      className: 'afterDeliveryRecord',
      data: {
        searchAll: true,
        afterssrid: this.data.paramObj.afterId,
        selectFields: ['id','logisticsName','odd','freight'],
        nbl:Base64.encode(util.timeFormatDay(new Date()))
      }
    }

    CommonService.goFindList(param).then(res=>{
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result;
        console.log('物流费用');
        console.log(result);

        //计算总费用
        let logisticsTotal=0;
        let arr=[];
        result.forEach(item => {
          if(item.freight){
            arr.push(item);
            logisticsTotal+=item.freight;
          }
        });

        this.setData({
          logisticsList: arr,
          logisticsTotal:logisticsTotal
        })
      }
    }).catch((error) => {
    })
  },

  //切换折叠面板
  onChange(event) {
    this.setData({
      activeNames: event.detail,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
